<html>
<head><meta charset="utf-8"><title>abi of llvm half · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html">abi of llvm half</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="164155054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164155054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164155054">(Apr 25 2019 at 09:19)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> Do you know what the ABI of LLVM half type is on other language frontends?</p>



<a name="164155121"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164155121" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164155121">(Apr 25 2019 at 09:20)</a>:</h4>
<p>AFAICT all assume that its layout is the same as <code>i16</code> and interface by passing <code>i16</code> through ABIs. The SysV spec does not mention half, so I have no idea whether this is correct.</p>



<a name="164155156"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164155156" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164155156">(Apr 25 2019 at 09:21)</a>:</h4>
<p>Here be dragons</p>



<a name="164155234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164155234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164155234">(Apr 25 2019 at 09:22)</a>:</h4>
<p>Because C doesn't have a standard or even nonstandard-yet-widely-adopted <code>half</code> type and many targets (including x86_64) have no special support for it, its ABI is likely a wild west mess.</p>



<a name="164156928"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164156928" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164156928">(Apr 25 2019 at 09:53)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> so clang errors when <code>half</code> is used in APIs</p>



<a name="164156946"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164156946" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164156946">(Apr 25 2019 at 09:53)</a>:</h4>
<p><a href="https://godbolt.org/z/gnPqjw" target="_blank" title="https://godbolt.org/z/gnPqjw">https://godbolt.org/z/gnPqjw</a></p>



<a name="164156956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164156956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164156956">(Apr 25 2019 at 09:53)</a>:</h4>
<p>that's... more sane that I had feared</p>



<a name="164157007"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157007" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157007">(Apr 25 2019 at 09:54)</a>:</h4>
<p>more careful, to be precise</p>



<a name="164157037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157037">(Apr 25 2019 at 09:54)</a>:</h4>
<p>it only allows passing them by memory</p>



<a name="164157183"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157183" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157183">(Apr 25 2019 at 09:56)</a>:</h4>
<p>in rust we can use <code>i16</code> to interface with the LLVM intrinsics just fine</p>



<a name="164157195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157195">(Apr 25 2019 at 09:57)</a>:</h4>
<p>appears that <code>i16</code> and <code>f16</code> have the same layout</p>



<a name="164157294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157294">(Apr 25 2019 at 09:59)</a>:</h4>
<p>for stuff like <code>fadd</code> we do need a bitcast</p>



<a name="164157411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157411">(Apr 25 2019 at 10:01)</a>:</h4>
<p>right but we could have Rust intrinsics like <code>fadd_f16h(i16, i16) -&gt; i16</code> that expand to bitcast -&gt; fadd -&gt; bitcast</p>



<a name="164157419"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157419" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157419">(Apr 25 2019 at 10:01)</a>:</h4>
<p>sure, that's what I meant - i was asked to write a proper constructive reply to the RFC</p>



<a name="164157422"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157422" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157422">(Apr 25 2019 at 10:01)</a>:</h4>
<p>If we really wanted to avoid introducing a new primitive type for binary16</p>



<a name="164157428"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157428" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157428">(Apr 25 2019 at 10:01)</a>:</h4>
<p>did not want to put anything wrong in there</p>



<a name="164157478"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157478" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157478">(Apr 25 2019 at 10:02)</a>:</h4>
<p>you mentions that implementing the type in a library could be problematic, if it extends to f32, but i don't think that's the only implementation possible</p>



<a name="164157489"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157489" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157489">(Apr 25 2019 at 10:02)</a>:</h4>
<p>like sure, a library could do that, and then it would have the problems you mention, but it could also just implement arithmetic without extending to f32 AFAICT</p>



<a name="164157514"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157514" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157514">(Apr 25 2019 at 10:03)</a>:</h4>
<p>that would most certainly incur a perf cost in most targets</p>



<a name="164157518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157518">(Apr 25 2019 at 10:03)</a>:</h4>
<p>Yeah that would be even worse for performance on the affected targets (those without native f16 instructions)</p>



<a name="164157521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157521">(Apr 25 2019 at 10:03)</a>:</h4>
<p>but then using f32 internally could be done with a <code>fast_math</code> cargo feature</p>



<a name="164157527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157527">(Apr 25 2019 at 10:03)</a>:</h4>
<p>I don't understand what problem you're trying to solve by that</p>



<a name="164157573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157573">(Apr 25 2019 at 10:04)</a>:</h4>
<p>Expanding to f32 for arithmetic is perfectly fine</p>



<a name="164157578"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157578" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157578">(Apr 25 2019 at 10:04)</a>:</h4>
<p>I thought IEEE754 required that to be opt-in</p>



<a name="164157581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157581">(Apr 25 2019 at 10:04)</a>:</h4>
<p>Ok hold on</p>



<a name="164157583"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157583" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157583">(Apr 25 2019 at 10:04)</a>:</h4>
<p>Otherwise the results would be different between a target that does that, and one that does have f16 arithmetic in hardware</p>



<a name="164157596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157596">(Apr 25 2019 at 10:05)</a>:</h4>
<p>IIRC that's IEEE754:2008 10.4</p>



<a name="164157641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157641">(Apr 25 2019 at 10:05)</a>:</h4>
<p>(in C it would be fine though, because C does not guarantee that a float is really 32-bit wide)</p>



<a name="164157813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157813">(Apr 25 2019 at 10:08)</a>:</h4>
<p>If you explicitly round back to bfloat16 after every single operation, then some operations are automatically correctly rounded and the rest are off in deterministic ways (independent of optimizer choices). So there's a reasonable level of predictability and reproducibility there, just not across different hardware, but that's already not true currently for f{32,64} for various reasons.</p>



<a name="164157915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157915">(Apr 25 2019 at 10:10)</a>:</h4>
<p>I guess my question is whether, e.g., <code>rn(a: f16 + b: f16)</code> produces the same result as <code>rn(a: f16 as f32 + b: f16 as f32) as f16</code> where a <code>rn</code> happens for the 32-bit addition, and then i suppose anotherone happens in the conversion back to f16</p>



<a name="164157940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164157940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164157940">(Apr 25 2019 at 10:11)</a>:</h4>
<p>IIRC for addition that does produce the same result. Multiplication and sqrt and so on I'm not sure about</p>



<a name="164158002"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158002" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158002">(Apr 25 2019 at 10:12)</a>:</h4>
<p>Furthermore, if we demand an actual correctly rounded (= software) implementation of everything, then I am really skeptical whether implementing that is economical (i.e., it would make me lean towards not providing the type portably at all). And why should we demand that? It's likely not going to help achieve reproduciblity because on hardware <em>with</em> native support for there are going to be all the same obstacles to reproducibility as we currently have with f32, f64.</p>



<a name="164158021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158021">(Apr 25 2019 at 10:12)</a>:</h4>
<p>I'm not suggesting that we should, only that we could provide different implementations</p>



<a name="164158022"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158022" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158022">(Apr 25 2019 at 10:13)</a>:</h4>
<p>Probably including not-quite-compliant hardware that you can't paper over except by massive slowdown, as with x87. Only nobody cares about x87 but bfloat16 implementations are hot right now.</p>



<a name="164158097"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158097" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158097">(Apr 25 2019 at 10:14)</a>:</h4>
<p>I think we shouldn't paper over x87, but we shouldn't make it constrain our design space either</p>



<a name="164158110"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158110" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158110">(Apr 25 2019 at 10:15)</a>:</h4>
<p>We can fix the precision of x87 o 24 or 5x mantissa bits by default, and offer a flag to let users do whatever they want, or provide a f80 type that explicitly does that, or...</p>



<a name="164158116"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158116" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158116">(Apr 25 2019 at 10:15)</a>:</h4>
<p>anyhow this is a different topic</p>



<a name="164158123"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158123" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158123">(Apr 25 2019 at 10:15)</a>:</h4>
<p>I think you have answered the question</p>



<a name="164158189"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158189" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158189">(Apr 25 2019 at 10:16)</a>:</h4>
<p>I think that while a library can do the <code>as f32</code> , if we were to provide intrinsics for f16 or bfloat16, these would deal in i16, and whether things are casted to f32 or not would be up to rustc to decide</p>



<a name="164158198"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158198" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158198">(Apr 25 2019 at 10:16)</a>:</h4>
<p>if we were to do that, I expect the intrinsics that rustc provides to be portable</p>



<a name="164158202"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158202" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158202">(Apr 25 2019 at 10:16)</a>:</h4>
<p>such that a library wouldn't need to do the casts</p>



<a name="164158289"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158289" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158289">(Apr 25 2019 at 10:18)</a>:</h4>
<p>It wouldn't need to, but it could (with fp_contract related caveats noted). That was my point: even with only target specific intrinsics in core::arch, a third party crate could provide a decent (if non-optimal) <em>portable</em> implementation of bfloat16</p>



<a name="164158430"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158430" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158430">(Apr 25 2019 at 10:21)</a>:</h4>
<p>To check that I understood. Those fp_contract caveats only would hold if LLVM can't apply them properly to whatever IR we would generate right ?</p>



<a name="164158448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158448">(Apr 25 2019 at 10:21)</a>:</h4>
<p>I think you have raised a similar point that I did not fully get. You mention that an advantage of a native type would be "arithmetic operators".</p>



<a name="164158510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158510">(Apr 25 2019 at 10:22)</a>:</h4>
<p>How is that different from implementing the <code>std::ops</code> (e.g. where <code>Add::add</code> just forwards to <code>core::intrinsics::fadd_f16</code> or similar) ?</p>



<a name="164158615"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158615" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158615">(Apr 25 2019 at 10:24)</a>:</h4>
<blockquote>
<p>To check that I understood. Those fp_contract caveats only would hold if LLVM can't apply them properly to whatever IR we would generate right ?</p>
</blockquote>
<p>Right, if we generate IR that works with <code>fadd bfloat16</code> or whatever without explicitly casting back and forth, then we're giving LLVM backends the option of not rounding after every operation. If we have the rounding steps explicitly in there, either because we used a "pure library implementation" or because rustc intrinsics expand to f16b adds to <code>cast; fadd float; cast</code>, then I don't think they can be eliminated.</p>



<a name="164158658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158658">(Apr 25 2019 at 10:25)</a>:</h4>
<blockquote>
<p>How is that different from implementing the <code>std::ops</code> (e.g. where <code>Add::add</code> just forwards to <code>core::intrinsics::fadd_f16</code> or similar) ?</p>
</blockquote>
<p>You can provide intrinsics <em>and</em> a wrapper type that implements them, but at that point as far as the user is concerned you might as well expose the type <em>instead of</em> the intrinsics and then the details are mostly noise (including the decision of whether it's an actual literal primitive type or a core-provided library type that's in the prelude)</p>



<a name="164158707"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158707" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158707">(Apr 25 2019 at 10:26)</a>:</h4>
<p>Well except for literals but ¯\_(ツ)_/¯</p>



<a name="164158738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158738">(Apr 25 2019 at 10:27)</a>:</h4>
<p>A couple of things.</p>
<p>So if we were to go the intrinsics way, i'd expect a <code>core::f16</code> module containing f16 operations, one would be <code>core::f16_as_f32(i16) -&gt; f32</code>, and another would be <code>core::f16::add(i16, i16) -&gt; i16</code>. </p>
<p>I don't expect rustc to expand add to cast, fadd, cast, but to an fadd on f16 (with bitcasts). If that does not work on all targets right now, then the library would need to work around that on that target (rustc could just emit a <code>unimplemented!</code> for those).</p>



<a name="164158818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158818">(Apr 25 2019 at 10:28)</a>:</h4>
<p>I think the issues in favor of a native type are literals, and <code>as</code> casts. But C++ does not have these issues because of user-defined literals and casts</p>



<a name="164158820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158820">(Apr 25 2019 at 10:28)</a>:</h4>
<p>And many rust libraries would benefit from those</p>



<a name="164158841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158841">(Apr 25 2019 at 10:29)</a>:</h4>
<p>A problem with providing <code>f16</code> native types is that we would need to pick a rust ABI, wouldn't be usable in <code>extern "C"</code>, etc. etc.</p>



<a name="164158849"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158849" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158849">(Apr 25 2019 at 10:29)</a>:</h4>
<p>I suppose these problems are minor</p>



<a name="164158915"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158915" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158915">(Apr 25 2019 at 10:30)</a>:</h4>
<p>Very. We don't need to pick any rust ABI because rust ABI is unstable, and rejecting it in <code>extern "C"</code> is like two lines of code added to improper_ctypes.</p>



<a name="164158916"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158916" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158916">(Apr 25 2019 at 10:30)</a>:</h4>
<p>but at the end our <code>f16</code> primitive type implementation wouldn't be much better than that of a library, and would be quite harder to iterate on, since it can't be in a crate</p>



<a name="164158920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158920">(Apr 25 2019 at 10:30)</a>:</h4>
<p>(because of a cyclic dep with core at least)</p>



<a name="164158923"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158923" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158923">(Apr 25 2019 at 10:30)</a>:</h4>
<p>although we could hack that</p>



<a name="164158955"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164158955" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164158955">(Apr 25 2019 at 10:31)</a>:</h4>
<p>I just don't see many advantages either way. We already have a crate for <code>f16</code>, if the issues with casts, perf, literals, are real, we might just want to extend the language to fix those for the <code>half</code> crate, and all other crates that would benefit.</p>



<a name="164159017"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159017" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159017">(Apr 25 2019 at 10:32)</a>:</h4>
<p>Oh sure for prototyping it would probably be great to do as much out of tree as possible but there's still the question of what interface we ultimately want to expose to end users and that should IMO be unclouded by development process concerns.</p>



<a name="164159021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159021">(Apr 25 2019 at 10:32)</a>:</h4>
<p>As in, adding a new primitive type is a big change, that for <code>f16</code> and <code>bfloat16</code> benefits a minority, while user-defined literals would be a more self-contained change, that would benefit everyone</p>



<a name="164159032"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159032" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159032">(Apr 25 2019 at 10:32)</a>:</h4>
<p>User-defined literals are a gigantic can of worms and I don't want to touch it</p>



<a name="164159037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159037">(Apr 25 2019 at 10:32)</a>:</h4>
<p>I think C++ solves this nicely.</p>



<a name="164159077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159077">(Apr 25 2019 at 10:33)</a>:</h4>
<p><code>trait UDL { const fn from_literal_string(s: &amp;str) -&gt; Self; } </code></p>



<a name="164159081"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159081" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159081">(Apr 25 2019 at 10:33)</a>:</h4>
<p><em>tiredly gestures at the extensive pre-RFC discussions</em></p>



<a name="164159139"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159139" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159139">(Apr 25 2019 at 10:34)</a>:</h4>
<p>oh i wasn't aware of those</p>



<a name="164159171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159171">(Apr 25 2019 at 10:34)</a>:</h4>
<p><a href="https://internals.rust-lang.org/t/pre-rfc-custom-literals-via-traits/8050" target="_blank" title="https://internals.rust-lang.org/t/pre-rfc-custom-literals-via-traits/8050">https://internals.rust-lang.org/t/pre-rfc-custom-literals-via-traits/8050</a> ?</p>



<a name="164159287"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159287" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159287">(Apr 25 2019 at 10:37)</a>:</h4>
<p>That is the one I had in mind but I can't swear there isn't more. If there is it's hopefully cross-referenced.</p>



<a name="164159386"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/abi%20of%20llvm%20half/near/164159386" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/abi.20of.20llvm.20half.html#164159386">(Apr 25 2019 at 10:39)</a>:</h4>
<p>ok so I read the RFC and was like, this is what i had in ming, read the comments, and is like udls should solve units of measure <span aria-label="laughter tears" class="emoji emoji-1f602" role="img" title="laughter tears">:laughter_tears:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>